#!/bin/bash

# 读取参数
projectName=$1
# 获取当前脚本的绝对路径
cd $PWD

PROJECT_DIR=~/Code/Goku/open-svg/projects/$projectName
IMAGES_DIR=$PROJECT_DIR/images
REMOTE_ADDR="mason@brandgo.cn"
REMOTE_DIR="~/open-svg/projects/$projectName"

echo "上传images文件夹到远程服务器"
echo "Uploading images directory to remote server..."
echo "Current directory: $PWD"
scp -r $IMAGES_DIR "${REMOTE_ADDR}:${REMOTE_DIR}"
scp ${PROJECT_DIR}/urls.json "${REMOTE_ADDR}:${REMOTE_DIR}/urls.json"

echo "执行远程编译脚本"
ssh mason@brandgo.cn "cd ~/open-svg/ && git reset --hard && git pull && node svg.js compile -n $projectName"

echo "获取code.txt到本地文件夹"
echo "Copying code.txt back to local directory..."
if ssh $REMOTE_ADDR "test -f ${REMOTE_DIR}/code.txt"; then
  scp "${REMOTE_ADDR}:${REMOTE_DIR}/code.txt" $PROJECT_DIR
else
  echo "Error: remote code.txt does not exist."
  exit 1
fi

echo "更新urls.json到本地文件夹"
echo "Copying code.txt back to local directory..."
if ssh $REMOTE_ADDR "test -f ${REMOTE_DIR}/urls.json"; then
  scp "${REMOTE_ADDR}:${REMOTE_DIR}/urls.json" $PROJECT_DIR
else
  echo "Error: remote code.txt does not exist."
  exit 1
fi

echo "All operations completed successfully."
